package com.xgh.offer;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @ClassName Offer30
 * @Description
 * @Author xinggh
 * @Date 2020/7/1 16:16
 * @Version 1.0
 **/
public class Offer30 {
    /**
     * 剑指 Offer 30. 包含min函数的栈
     * 定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
     * <p>
     * <p>
     * <p>
     * 示例:
     * <p>
     * MinStack minStack = new MinStack();
     * minStack.push(-2);
     * minStack.push(0);
     * minStack.push(-3);
     * minStack.min();   --> 返回 -3.
     * minStack.pop();
     * minStack.top();      --> 返回 0.
     * minStack.min();   --> 返回 -2.
     * <p>
     * <p>
     * 提示：
     * <p>
     * 各函数的调用总次数不超过 20000 次
     */
    Deque<Integer> deque;
    Deque<Integer> minDeque;

    public Offer30() {
        deque = new LinkedList<>();
        minDeque = new LinkedList<>();

    }


    public void push(int x) {
        deque.push(x);
        if (minDeque.isEmpty()) {
            minDeque.push(x);
        } else {
            Integer last = minDeque.getFirst();
            if (last >= x) {
                minDeque.push(x);
            }
        }
    }

    public void pop() {
        int pop = deque.pop();
        int last = minDeque.peekFirst();
        if (pop == last) {
            minDeque.pop();
        }
    }

    public int top() {
        return deque.peekFirst();
    }

    public int min() {

        return minDeque.peekFirst();
    }

    public static void main(String[] args) {
        Offer30 offer30 = new Offer30();
        offer30.push(85);
        offer30.push(-99);
        offer30.push(67);
        // System.out.println(offer30.min());\
        System.out.println(offer30.min());
        offer30.pop();
        System.out.println(offer30.top());
        System.out.println(offer30.min());

    }
}
